package com.github.aloxc.plugin.restplus.entity;

import com.github.aloxc.plugin.DonotGuard;
import com.github.aloxc.plugin.restplus.method.HttpMethod;
import com.github.aloxc.plugin.restplus.utils.JsonUtils;
import com.github.aloxc.plugin.restplus.test.TestResult;

import java.io.Serializable;
import java.util.*;
/**
 *
 * @author leero
 */
@DonotGuard
public class RequestHistory implements Serializable {
    private static final long serialVersionUID = 5609984610120581467L;
    @DonotGuard
    private String url;
    @DonotGuard
    private String service;
    @DonotGuard
    private String test;
    @DonotGuard
    private Map<String, String> headerMap;
    @DonotGuard
    private Map<String, String> cookieMap;
    @DonotGuard
    private Map<String, String> paramMap;
    @DonotGuard
    private String body;
    @DonotGuard
    private HttpMethod httpMethod;
    @DonotGuard
    private String message;
    @DonotGuard
    private Date time;
    @DonotGuard
    private String content;
    @DonotGuard
    private int statusCode;
    @DonotGuard
    private List<TestResult> testResultList;


    public RequestHistory() {
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

    public Map<String, String> getHeaderMap() {
        return headerMap;
    }

    public void setHeaderMap(Map<String, String> headerMap) {
        this.headerMap = headerMap;
    }

    public Map<String, String> getCookieMap() {
        return cookieMap;
    }

    public void setCookieMap(Map<String, String> cookieMap) {
        this.cookieMap = cookieMap;
    }

    public Map<String, String> getParamMap() {
        return paramMap;
    }

    public void setParamMap(Map<String, String> paramMap) {
        this.paramMap = paramMap;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public List<TestResult> getTestResultList() {
        return testResultList;
    }

    public void setTestResultList(List<TestResult> testResultList) {
        this.testResultList = testResultList;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public HttpMethod getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(HttpMethod httpMethod) {
        this.httpMethod = httpMethod;
    }

    public static RequestHistory fromJson(String json) {
        Map<String, Object> _root = JsonUtils.toBean(json, HashMap.class);
        RequestHistory history = new RequestHistory();
        history.setTest((String) _root.get("test"));
        history.setUrl((String) _root.get("url"));
        history.setService((String) _root.get("service"));
        history.setBody((String) _root.get("body"));
        history.setMessage((String) _root.get("message"));
        history.setContent((String) _root.get("content"));
        history.setStatusCode((int) _root.get("statusCode"));
        history.setTime((Date) _root.get("time"));
        history.setHttpMethod(HttpMethod.getByRequestMethod((String) _root.get("httpMethod")));
        history.setParamMap((Map<String, String>) _root.get("paramMap"));
        history.setCookieMap((Map<String, String>) _root.get("cookieMap"));
        history.setHeaderMap((Map<String, String>) _root.get("headerMap"));
        List<TestResult> testResultList = new ArrayList<>();
        if (_root.get("testResultList") != null) {
            for (Object result : (List) _root.get("testResultList")) {
                Map<String, ?> r = (Map<String, ?>) result;
                TestResult testResult = new TestResult();
                testResult.setMessage((String) r.get("message"));
                testResult.setName((String) r.get("name"));
                testResult.setSuccess((Boolean) r.get("success"));
                testResultList.add(testResult);
            }
        }
        history.setTestResultList(testResultList);
        return history;
    }
}
